package  
{
	import flash.display.*;
	/**
	 * Verlet 线段
	 * @author StefanieKaka
	 */
	public class VerletStick 
	{
		/// 构造函数
		public function VerletStick(pointA : VerletPoint, pointB : VerletPoint, length : Number = -1) 
		{
			m_pointA = pointA;
			m_pointB = pointB;
			
			if (length == -1)
			{
				var dx : Number = pointA.x - pointB.x;
				var dy : Number = pointA.y - pointB.y;
				m_length = Math.sqrt(dx * dx + dy * dy);
			}
			else
			{
				m_length = length;
			}
		}
		
///////////////////////////////////////////////////////////////////////////////////////////////////
// 公共域
///////////////////////////////////////////////////////////////////////////////////////////////////
		
		/// 更新
		public function update() : void
		{
			var dx : Number = m_pointB.x - m_pointA.x;
			var dy : Number = m_pointB.y - m_pointA.y;
			var dist : Number = Math.sqrt(dx * dx + dy * dy);
			var diff : Number = m_length - dist;
			var offsetX : Number = (diff * dx / dist) / 2;
			var offsetY : Number = (diff * dy / dist) / 2;
			m_pointA.x -= offsetX;
			m_pointA.y -= offsetY;
			m_pointB.x += offsetX;
			m_pointB.y += offsetY;
		}
		
		/// 渲染
		public function render(g : Graphics) : void
		{
			g.lineStyle(0);
			g.moveTo(m_pointA.x, m_pointA.y);
			g.lineTo(m_pointB.x, m_pointB.y);
		}

///////////////////////////////////////////////////////////////////////////////////////////////////
// 私有域
///////////////////////////////////////////////////////////////////////////////////////////////////
		
		private var m_pointA : VerletPoint = null;
		private var m_pointB : VerletPoint = null;
		private var m_length : Number = NaN;
		
	}

}